package es.udc.emarketapp.http.controller.actions.shippings;

import es.udc.emarketapp.emarket.model.shippingFacade.ShippingFacade;
import es.udc.emarketapp.emarket.model.shippingFacade.vo.ShippingsSellerChunkVO;
import es.udc.emarketapp.http.view.actionForms.FindShippingForm;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class FindShippingsAction extends DefaultAction {

    public ActionForward doExecute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        /* Get data. */
        FindShippingForm findForm = (FindShippingForm) form;
        String idSeller = findForm.getIdSeller();
        int startIndex = findForm.getStartIndex();
        int count = findForm.getCount();

        if(request.getAttribute("idSellerShippings")!=null){
            idSeller = (String)request.getAttribute("idSellerShippings");
        }

        ShippingFacade shippingFacade = new ShippingFacade();
        Locale locale = getLocale(request);
        ShippingsSellerChunkVO shippingsChunkVO = 
                shippingFacade.findCountriesRatesBySeller(idSeller, locale.getLanguage(),
                startIndex, count);


        if (shippingsChunkVO.getCountriesToSendVOs().size() > 0) {
            request.setAttribute("shippingsCountry",
                    shippingsChunkVO.getCountriesToSendVOs());
        }

        /* Generate parameters for previous and next links. */
        Map previousLinkParameters = getPreviousLinkParameters(idSeller,
                startIndex, count);


        if (previousLinkParameters != null) {
            request.setAttribute("previous", previousLinkParameters);

        }

        if (shippingsChunkVO.isExistMoreCountries()) {
            Map nextLinkParameters = getNextLinkParameters(idSeller,
                    startIndex, count);
            request.setAttribute("next", nextLinkParameters);

        }

        request.setAttribute("idSeller", idSeller);
        return mapping.findForward("success");

    }

    private Map getPreviousLinkParameters(String idSeller,
            int startIndex, int count) {

        Map linkAttributes = null;

        if ((startIndex - count) > 0) {

            linkAttributes = getCommonPreviousNextLinkParameters(idSeller,
                    startIndex, count);
            linkAttributes.put("startIndex", new Integer(startIndex - count));

        }

        return linkAttributes;


    }

    private Map getNextLinkParameters(String idSeller, int startIndex,
            int count) {

        Map linkAttributes = getCommonPreviousNextLinkParameters(idSeller,
                startIndex, count);

        linkAttributes.put("startIndex", new Integer(startIndex + count));

        return linkAttributes;
    }

    private Map getCommonPreviousNextLinkParameters(String idSeller,
            int startIndex, int count) {

        Map linkAttributes = new HashMap();

        linkAttributes.put("idSeller", idSeller);
        linkAttributes.put("count", new Integer(count));

        return linkAttributes;

    }
}
